package com.bezdelnick.forum.web.panel;

import com.bezdelnick.forum.util.PropertyUtils;
import com.bezdelnick.forum.web.page.UploadFilesPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.MultiFileUploadField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.file.Files;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 03.09.12
 * Time: 11:38
 * To change this template use File | Settings | File Templates.
 */
public class UploadFilesPanel extends Panel {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadFilesPanel.class);

    private final Collection<FileUpload> uploads = new ArrayList<FileUpload>();

    public UploadFilesPanel(String id) {
        super(id);

        Form form = new Form("form") {
            @Override
            protected void onSubmit() {
                for (FileUpload upload : uploads) {
                    File newFile = new File(getUploadFolder(), upload.getClientFileName());

                    checkFileExists(newFile);
                    try {
                        newFile.createNewFile();
                        upload.writeTo(newFile);
                        LOGGER.info("saved file: " + upload.getClientFileName());
                    } catch (Exception e) {
                        LOGGER.warn("Unable to write file");
                        LOGGER.warn(e.getMessage());
//                        throw new IllegalStateException("Unable to write file");
                    }
                }
            }
        };
        add(form);

        form.add(new MultiFileUploadField("add", new PropertyModel(this, "uploads"), 20));

        Button btnClearListFiles = new Button("clear", new Model<String>("Clear list")) {
            @Override
            public void onSubmit() {
                super.onSubmit();
                uploads.removeAll(new ArrayList<FileUpload>());
            }
        };
        form.add(btnClearListFiles);

        Button btnCancel = new Button("cancel", new Model<String>("")) {
            @Override
            public void onSubmit() {
                super.onSubmit();
                uploads.removeAll(new ArrayList<FileUpload>());
                setResponsePage(new UploadFilesPage());
            }
        };
        form.add(btnCancel);
    }

    private String getUploadFolder() {
        Properties properties = PropertyUtils.loadFromClassPath("application.properties", false);
        String location = properties.getProperty("repository.location");
        return location;
    }

    private void checkFileExists(File newFile) {
        if (newFile.exists()) {
            // Try to delete the file
            if (!Files.remove(newFile)) {
                throw new IllegalStateException("Unable to overwrite " + newFile.getAbsolutePath());
            }
        }
    }
}
